import time
import os
import sys
from selenium.webdriver.common.by import By

# 将项目根目录（BlogAutoTest）加入搜索路径
# 脚本路径：tests/BlogLogin.py → 向上一级到 BlogAutoTest
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root_dir)

# 绝对导入（从根目录开始找 common 包）
from common.Utils import BlogDriver

#测试博客登录页面
class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    #成功登陆的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #能够找到博客首页的昵称说明登陆成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        #屏幕截图
        BlogDriver.getScreenShot()
        #返回到登陆页面
        self.driver.back()

    #异常登陆的测试用例
    def LoginFailTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        #错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1233")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #屏幕截图
        BlogDriver.getScreenShot()

login = BlogLogin()
login.LoginSucTest()
login.LoginFailTest()




